const cache = new Map()

export const cachedRequest = (fn, key, ttl = 300000) => {
  return async (...args) => {
    const cacheKey = typeof key === 'function' ? key(...args) : key
    const now = Date.now()
    
    if (cache.has(cacheKey)) {
      const { data, timestamp } = cache.get(cacheKey)
      if (now - timestamp < ttl) {
        return data
      }
    }
    
    const result = await fn(...args)
    cache.set(cacheKey, { data: result, timestamp: now })
    return result
  }
}